﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess.Repository.Interface;
using DataAccess.Repository;

namespace KPI
{
    public partial class PolicyDocuments :  BasePage
    {
        private IPolicyDocuments ipd = new IMPPolicyDocument();


        protected int PolicyDocumentID
        {
            get
            {
                return CommonHelper.QueryStringInt("id");
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {
                var u = KPISiteContext.Current.CurrentUser;
                if (u != null && u.RoleName != "administrators")
                {
                    Response.Redirect("~/PolicyDocumentsList.aspx");
                }

                if (PolicyDocumentID > 0)
                {
                  btnReturn.Visible = true;
                  DataAccess.Models.PolicyDocuments pdDetail=ipd.GetDetailPolicyDocuments(PolicyDocumentID);
                  if (pdDetail != null)
                  {
                      this.pdAddtime.Value = pdDetail.PolicyDocumentAddTime.Value.ToString();
                      this.PolicyDocumentTitle.Text = pdDetail.PolicyDocumentTitle;
                      //this. = pdDetail.PolicyDocumentAddTime.Value.ToString("yyyy-MM-dd HH:mm");
                      this.PolicyDocumentContent.Text = pdDetail.PolicyDocumentContent;
                  }
                }
            }
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if (PolicyDocumentID > 0)
            {
                Title = "编辑通知公告";
                lTitle.Text = "编辑通知公告";
            }
            else
            {
                Title = "添加通知公告";
                lTitle.Text = "添加通知公告";
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            DataAccess.Models.PolicyDocuments pd = new DataAccess.Models.PolicyDocuments();
            if (PolicyDocumentID > 0)
            {
                pd.PolicyDocumentID = PolicyDocumentID;
                pd.PolicyDocumentAddTime = Convert.ToDateTime(this.pdAddtime.Value);
                pd.PolicyDocumentModifyTime = DateTime.Now;
                pd.PolicyDocumentTitle = this.PolicyDocumentTitle.Text;
                pd.PolicyDocumentContent = this.PolicyDocumentContent.Text;
                ipd.EditPolicyDocument(pd);
                Response.Redirect("~/PolicyDocumentsDetail.aspx?pdid=" + PolicyDocumentID.ToString());
            }
            else
            {
                pd.PolicyDocumentAddTime = DateTime.Now;
                pd.PolicyDocumentModifyTime = DateTime.Now;
                pd.PolicyDocumentTitle = this.PolicyDocumentTitle.Text;
                pd.PolicyDocumentContent = this.PolicyDocumentContent.Text;
                ipd.CreatePolicyDocument(pd);
                Response.Redirect("~/PolicyDocumentsList.aspx");
            }
        }

        protected void btnReturn_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/PolicyDocumentsDetail.aspx?pdid=" + PolicyDocumentID.ToString());
        }
    }
}